package com.jsgt.greekfire.ui.trading.otc

import android.os.Bundle
import android.text.InputFilter
import android.text.TextUtils
import android.view.KeyEvent
import android.view.WindowManager
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.PriceFormatUtil
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.util.*
import com.jsgt.greekfire.factory.OTCTraddingModelFactory
import com.jsgt.greekfire.viewmodel.OTCTraddingViewModel
import kotlinx.android.synthetic.main.activity_otc_buy_commission.*
import kotlinx.android.synthetic.main.activity_otc_sell_commission.*
import kotlinx.android.synthetic.main.activity_otc_sell_commission.btnSaleEntrust
import kotlinx.android.synthetic.main.activity_otc_sell_commission.etNum
import kotlinx.android.synthetic.main.activity_otc_sell_commission.etPwd
import kotlinx.android.synthetic.main.activity_otc_sell_commission.etUnitPrice
import kotlinx.android.synthetic.main.activity_otc_sell_commission.tvCoinCode
import kotlinx.android.synthetic.main.activity_otc_sell_commission.tvusdtNun
import org.jetbrains.anko.backgroundColor
import java.math.BigDecimal

class OTCSellCommissionActivity : BaseActivity() {
    var tvTitle: TextView? = null
    var title_layout: RelativeLayout? = null
    var price: String? = null
    var dealAmount: String? = null

    var assetBalance: String? = null
    var bundle: Bundle? = null
    var coinCode: String? = null //币种
    override fun getLayoutId(): Int = R.layout.activity_otc_sell_commission
    private val otcTraddingViewModel: OTCTraddingViewModel by lazy {
        ViewModelProvider(
            this,
            OTCTraddingModelFactory()
        ).get(OTCTraddingViewModel::class.java)
    }

    override fun main() {
        bundle = intent.extras
        coinCode = bundle?.getString("coinCode")
        NetDataManager.getUserAssets()
        otcTraddingViewModel.getCoinlistData()
        tvTitle = findViewById(R.id.title_tv)
        etUnitPrice?.requestFocus()
        etUnitPrice?.setFilters(arrayOf<InputFilter>(DecimalDigitsInputFilter(2)))
        title_layout = findViewById(R.id.title_layout)
        tvTitle?.text = resources.getString(R.string.sell_order)
        tvCoinCode.setText(coinCode)
        title_layout?.backgroundColor = resources.getColor(R.color.white)
        otcTraddingViewModel.coinlistData.observe(this, Observer {
//            tvUnitPrice?.text = it.list!![0].dealAmount!!.toString()
//            tvusdtNun?.text = it.list!![0].dealAmount!!.toString()
//            dealAmount = it.list!![0].dealAmount!!.toString()
            it.result?.forEach { it1 ->
                dealAmount = it1.dealAmount.toString()
                tvusdtNun?.text = dealAmount
                etUnitPrice?.setText(dealAmount)
            }
        })


        btnSaleEntrust.setOnClickListener { v ->
            if (TextUtils.isEmpty(etUnitPrice.text.toString().trim())) {
                showToast(resources.getString(R.string.sell_unit_sold_hint))
                return@setOnClickListener
            }else if (TextUtils.isEmpty(etNum.text.toString().trim())) {
                showToast(resources.getString(R.string.sell_number_sales_hint))
                return@setOnClickListener
            }else if (TextUtils.isEmpty(etPwd?.text.toString().trim())) {
                showToast(resources.getString(R.string.sell_gfc_text7))
                return@setOnClickListener
            }
            hideSoftKeyboard(this)
            if (!TextUtils.isEmpty(assetBalance)) {
                var assbalance = assetBalance!!.toDouble()
                var price = etUnitPrice.text.toString().trim().toDouble()
                var num = etNum.text.toString().trim().toDouble()
                var balance = UtilsBigDecimal.mul(price, num, BigDecimal.ROUND_DOWN)
                if (balance > assbalance) {
                    ToastUtils.show("可用余额不足")
                    return@setOnClickListener
                }
            }
            showProgress(true)
            otcTraddingViewModel.getAddAd(
                1, etNum?.text.toString().trim(), etUnitPrice?.text.toString().trim()
                , MD5Util.getMD5(etPwd?.text.toString().trim()), coinCode!!
            )
        }
        otcTraddingViewModel.addAdData.observe(this, Observer {
            showProgress(false)
            finish()
            ToastUtils.show(it.toString())
        })
        otcTraddingViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
            ToastUtils.show(it.toString())
        })



        for (bean in NetDataManager.assetsList) {

            if (bean.assetName.equals(coinCode)) {
                assetBalance = PriceFormatUtil.format(bean.assetBalance, 4)
                etNum?.setHint(
                    "可用 " + PriceFormatUtil.format(
                        bean.assetBalance,
                        4
                    )
                )
            }

        }

    }

}